package com.syt.order.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syt.client.hospital.HospitalFeignClient;
import com.syt.enums.PaymentStatusEnum;
import com.syt.helper.HttpRequestHelper;
import com.syt.model.order.OrderInfo;
import com.syt.model.order.PaymentInfo;
import com.syt.order.mapper.PaymentInfoMapper;
import com.syt.order.service.OrderInfoService;
import com.syt.order.service.PaymentInfoService;
import com.syt.vo.order.SignInfoVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author XXXX
 * @since 2023-02-26
 */
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private HospitalFeignClient hospitalFeignClient;

    /**
     * 获取支付记录
     * @param orderId
     * @param paymentType
     * @return
     */
    @Override
    public PaymentInfo getPaymentRecords(Long orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id", orderId)
                .eq("payment_type", paymentType);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }

    /**
     * 支付成功
     * @param out_trade_no
     * @param status
     * @param resultMap
     */
    @Transactional
    @Override
    public void paySuccess(String out_trade_no, Integer status, Map<String, String> resultMap) {
        //1、根据订单编号查询支付记录
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("out_trade_no", out_trade_no);
        wrapper.eq("payment_type", status);
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        //2、更新支付记录信息
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackContent(resultMap.toString());
        baseMapper.updateById(paymentInfo);
        //3、根据订单id查询订单信息
        OrderInfo orderInfo = orderInfoService.getOrdersById(paymentInfo.getOrderId());
        orderInfo.setOrderStatus(PaymentStatusEnum.PAID.getStatus());
        //4、更新订单信息
        orderInfoService.updateById(orderInfo);

        //5、调用医院接口，更新订单支付信息
        SignInfoVo signInfoVo = hospitalFeignClient.getSignInfoVo(orderInfo.getHoscode());
        //封装
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("hoscode",orderInfo.getHoscode());
        reqMap.put("hosRecordId",orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);
        JSONObject result = HttpRequestHelper.sendRequest(reqMap, signInfoVo.getApiUrl()+"/order/updatePayStatus");

    }

    /**
     * 添加微信支付记录
     * @param orderInfo
     * @param paymentType
     */
    @Override
    public void savePaymentInfo(OrderInfo orderInfo,Integer  paymentType) {
      //1、查询订单id和支付类型，查询记录表中是否有相同订单
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<PaymentInfo>();
        queryWrapper.eq("order_id",orderInfo.getId());
        queryWrapper.eq("payment_type",paymentType);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count>0) return;
       //2、向添加支付记录中添加
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }
}
